<?php
/*
 * @Author: your name
 * @Date: 2022-01-20 16:26:40
 * @LastEditTime: 2022-03-06 22:34:43
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /nlog/app/Models/crm_model/CrmLinkman.php
 */

namespace App\Models\crm_model;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CrmColumn extends Model
{
    use HasFactory;
    static string $tableName = 'crm_column';
    
  
   
    public static function columnList(){
        return DB::table(self::$tableName)->select()->get();
    }

    public static function columnPermission(){
        return DB::select('SELECT `crm_column`.*,GROUP_CONCAT(`crm_permission`.`permission` ORDER BY  `crm_permission`.`sort` ASC,`crm_permission`.`id` ASC ) as `operation`,GROUP_CONCAT(`crm_permission`.`id` ORDER BY `crm_permission`.`sort` ASC,`crm_permission`.`id` ASC) as `operation_id`   FROM `crm_column` LEFT JOIN `crm_permission` ON `crm_column`.`id` = `crm_permission`.`column_id` GROUP BY `crm_column`.`id` ');
    }

    public static function permissionList($role_id){
        return DB::select('SELECT `crm_column`.*,GROUP_CONCAT(`crm_permission`.`permission`) as `permission` FROM `crm_role` INNER  JOIN `crm_column` ON  find_in_set(`crm_column`.`id`,`crm_role`.`column_id`) LEFT JOIN `crm_permission` ON `crm_permission`.`column_id` = `crm_column`.`id` AND  find_in_set(`crm_permission`.`id`,`crm_role`.`permission_id`) WHERE `crm_role`.`id` = '.$role_id.' GROUP BY `crm_column`.`id` ');
    } 
    public static function permissionColumn(){
        return DB::select(' SELECT `crm_column`.*,GROUP_CONCAT( `crm_permission`.`permission` ) as `permission`  FROM  `crm_column` LEFT JOIN `crm_permission` ON `crm_permission`.`column_id` = `crm_column`.`id` GROUP BY `crm_column`.`id` ');
    }
   
   
}

